package com.fan.teamer.process;

import java.util.Date;

import org.apache.log4j.Logger;

import com.fan.teamer.global.TeamerGlobalVariables;
import com.fan.teamer.model.domain.Event;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Team;
import com.fan.teamer.service.EventService;
import com.fan.teamer.service.MemberService;
import com.fan.teamer.service.TeamService;

public class EventCreateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger("EventCreateProcess");
	private Team team;
	private Member operator;

	private TeamService teamService;
	private MemberService memberService;
	private EventService eventService;

	private int id;
	private String name;
	private String description;
	private String type;
	private String location;
	private int isRecurring;
	private int isPublic;
	private Date eventTime;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		EventCreateProcess.logger = logger;
	}

	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public Member getOperator() {
		return operator;
	}

	public void setOperator(Member operator) {
		this.operator = operator;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public EventService getEventService() {
		return eventService;
	}

	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public int getIsRecurring() {
		return isRecurring;
	}

	public void setIsRecurring(int isRecurring) {
		this.isRecurring = isRecurring;
	}

	public int getIsPublic() {
		return isPublic;
	}

	public void setIsPublic(int isPublic) {
		this.isPublic = isPublic;
	}

	public Date getEventTime() {
		return eventTime;
	}

	public void setEventTime(Date eventTime) {
		this.eventTime = eventTime;
	}

	public int initCheck() {
		if (teamService == null || memberService == null
				|| eventService == null) {
			setErrorMessage("Service is null!");
			return TeamerGlobalVariables.FAILED;
		}

		if (team == null || operator == null) {
			setErrorMessage("Domain cannot be null!");
			return TeamerGlobalVariables.FAILED;
		}

		if (!teamService.hasTeam(team.getId())) {
			setErrorMessage("Team " + team.getName() + " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}
		if (!memberService.hasMember(operator.getEmail())) {
			setErrorMessage("Member " + operator.getEmail() + " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}

		if (!teamService.hasMember(team, operator)) {
			setErrorMessage("Team " + team.getName() + " doesn't have Member "
					+ operator.getEmail());
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

	@Override
	public Integer processImpl() {
		if (initCheck() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}

		// any role can create event
		Event event = new Event();

		event.setEventName(name);
		event.setTeamId(team.getId());
		event.setEventTime(eventTime);
		event.setDescription(description);
		event.setType(type);
		event.setLocation(location);
		event.setIsRecurring(isRecurring);
		event.setIsPublic(isPublic);
		event.setInitiator(operator.getEmail());

		try {
			eventService.addEvent(event);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage("Event " + event.getEventName()
					+ " cannot be saved");
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

}
